package sist.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import sist.bean.PageRank;

//excel
@Component
public class PageRanksView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> map,
			HSSFWorkbook workBook, HttpServletRequest arg2, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		HSSFSheet sheet=createFirstSheet(workBook);
		createColumLabel(sheet);
		List<PageRank>list=(List<PageRank>) map.get("pageRanks");
		int rowNum=1;
		for(PageRank rank:list){
			createPageRankRow(sheet, rank, rowNum++);
		}
	}

	public HSSFSheet createFirstSheet(HSSFWorkbook workBook){
		HSSFSheet sheet=workBook.createSheet();
		workBook.setSheetName(0, "si");
		sheet.setColumnWidth(1, 50*20);
		
		return sheet;
	}
	
	public void createColumLabel(HSSFSheet sheet){
		HSSFRow firstRow=sheet.createRow(0);
		HSSFCell cell=firstRow.createCell(0);
				 cell.setCellValue("count");
				 cell=firstRow.createCell(1);
				 cell.setCellValue("email");
				 cell=firstRow.createCell(2);
				 cell.setCellValue("name");
				 cell=firstRow.createCell(3);
				 cell.setCellValue("address");
		
	}
	
	public void createPageRankRow(HSSFSheet sheet,PageRank rank,int rowNum){
		HSSFRow row=sheet.createRow(rowNum);
		HSSFCell cell=row.createCell(0);
				 cell.setCellValue(rank.getCnt());
				 cell=row.createCell(1);
				 cell.setCellValue(rank.getEmail());
				 cell=row.createCell(2);
				 cell.setCellValue(rank.getName());
				 cell=row.createCell(3);
				 cell.setCellValue(rank.getAddress());
	}
}
